/**
 * Created by 邵亚楠 on 2018/9/7.
 */
var sessionId ;
var sessionId = 'oQ_An0pr2htuLDV42lwE1LeRM220'
$(function () {
   var count =  sessionStorage.getItem("count");
    if(count ==null){
        sessionId =  getQueryString('sessionid')
        sessionStorage.setItem('sessionId',sessionId)
        sessionStorage.setItem("count",1);
    }
    getDate()

})
//$(function () {
// var newData =  sessionStorage.getItem("count");
//  if(newData !=null&&newData == 1&&newData != 2){
//       sessionId =  getQueryString('sessionid')
//      sessionStorage.setItem('sessionId',sessionId)
//      if(sessionId!=null){
//          sessionStorage.setItem("count",2);
//          //alert(sessionId)
//      }
//  }
//  if(newData == null || newData == 0){
//      sessionStorage.setItem("count",1);
//  console.log(newData)
//      $.ajax({
//          url:"https://www.bjdeshangyuan.com/book/api/code",
//          type:"POST",
//          data:{'returnUrl':'http://www.bjdeshangyuan.com/yinguzhengjin.html'},
//          success:function(r){
//              var getData = r.data.redirect;
//              console.log(getData)
//              window.location.href=getData;
//          }
//      })
//  }
//  getDate()
//
//})
//文章列表
function getDate() {
    $.ajax({
        url: "https://www.bjdeshangyuan.com/book/api/content/list",
        type: 'POST',
        dateType: 'json',
        data: {page: 1, pageSize: 10},
        success: function (r) {
            if (r.code == 'USR000') {
                var newList = r.data.list;
                for (var i = 0; i < newList.length; i++) {
                    var id = newList[i].id
                    //console.log(id)
                    var img = newList[i].thumbnail
                    var html = '<div class="item">' +
                        '<a href="javascript:;" class="tiaozhuan" onclick="getNew(' + id + ')">' +
                        '<div class="item_detail">' +
                        '<h3>'+newList[i].title+'</h3>' +
                        '<p class="content">'+newList[i].text+'</p>' +
                        '<p class="browse_box"><span class="browse">'+newList[i].browseTimes+'</span><span class="like_box" data-id = '+id+'><span class="like"></span><span class="like_count">'+newList[i].thumbUpTimes+'</span></span></p>' +
                        '</div>' +
                        '<div class="pic_r">' +
                        '<img src="http://www.bjdeshangyuan.com/book'+img+'" alt=""/>' +
                        '</div>' +
                        '</a>' +

                        '</div>'

                    $('.list_box').append(html)

                }

                $('.like_box').click(function(event){
                    event.stopPropagation();
                })

            }

        }
    })
}

function getNew(id) {
    window.location.href = "article.html?id=" + id + ""
}

//文章点赞
//function getLike(id){
//    $.ajax({
//        url:'https://www.bjdeshangyuan.com/book/api/content/thumbUp',
//        type:'POST',
//        dataType:'json',
//        data:{'sessionid':sessionId,'contentId':id},
//        success:function(r){
//                //console.log(r)
//            if(r.code == "USR000"){
//                var likeCount = r.data.thumbUpTimes
//                var $ref = $('.like_box')
//                $ref.each(function(){
//                  if($(this).attr('data-id') == id){
//                      //$(this).find('.like').toggleClass('bgc')
//                      $(this).find('.like_count').text(likeCount)
//                  }
//                })
//            }else{
//                console.log('错误')
//            }
//
//        }
//    })
//}

function getQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]);
    return null;
}

